-- UIHeroGainEffect
-- Create by chengb Jan/5/2015
-- 获得英雄特效界面

require "game/ui/form/hero/UIHeroDetailInfo"

-- 模型缩放比例
local HERO_MODEL_SCALE = 1

-- UIHeroGainEffect继承自Layer
UIHeroGainEffect = class("UIHeroGainEffect", function()
    return cc.Layer:create();
end);

function UIHeroGainEffect.create(classId)
    return UIHeroGainEffect.new(classId);
end

-- 内部函数声明
local resize;
local redraw;
local redrawStar;
local registerTouchEvent;

-- 构造函数
function UIHeroGainEffect:ctor(classId)
    -- 初始化
    self:setName("UIHeroGainEffect");
    local node = cc.CSLoader:createNode("layout/hero/HeroGainEffect.csb");
    self:addChild(node);
    self.node = node;

    self.classId = classId;

    -- 需要显示在头部菜单上面
    self:setLocalZOrder(UIMgr.BONUS_NOTIFY_ZORDER + 1);

    -- 重绘
    self:redraw();

    -- 注册点击事件
    self:registerTouchEvent(node);

    -- 注册事件处理回调函数
    self:registerEventCallback();

    -- 适配
    self:resize();
end

-- 隐藏冒泡
function UIHeroGainEffect:hideBubble()
    local btnShare = findChildByName(self.node, "BT/btn_share");
    local hintNode = findChildByName(btnShare, "hint");
    checkBlueBubbleStatus(hintNode, false);
    setButtonEnabled(btnShare, false);
end

-- 注册事件处理回调函数
function UIHeroGainEffect:registerEventCallback()
    -- 关注facebook分享成功的事件
    EventMgr.register("UIHeroGainEffect", event.FB_SHARE_SUCC, function(args)
        -- 隐藏冒泡
        self:hideBubble();
    end);

    -- 关注line分享成功的事件
    EventMgr.register("UIHeroGainEffect", event.LINE_SHARE_SUCC, function(args)
        -- 隐藏冒泡
        self:hideBubble();
    end);

    -- 关注微信分享成功的事件
    EventMgr.register("UIHeroGainEffect", event.WECHAT_SHARE_SUCC, function(args)
        -- 隐藏冒泡
        self:hideBubble();
    end);

    -- 关注微博分享成功的事件
    EventMgr.register("UIHeroGainEffect", event.WEIBO_SHARE_SUCC, function(args)
        -- 隐藏冒泡
        self:hideBubble();
    end);

    self:registerScriptHandler(function(ev)
        if ev == "exit" then
            cclog("UIHeroGainEffect 界面析构清理");
            EventMgr.removeAll("UIHeroGainEffect");

            if UIDungeonMgr:getCurLevel() then
                UIDungeonMgr:getCurLevel():showTop();
            end
        elseif ev == "enter" then
            if UIDungeonMgr:getCurLevel() then
                UIDungeonMgr:getCurLevel():hideTop();
            end

            AudioM.playFx("get_slime");
        end
    end);
end

-- 显示英雄详细信息
function UIHeroGainEffect:showDetail()
    if UIMgr.getCurrentScene():isOpen("UIHeroDetailInfo") then
        UIMgr.getCurrentScene():removeFormByName("UIHeroDetailInfo");
    end

    if MarketM.isMarketSoldHero(self.classId) ~= true then
        local uiHeroInfo = UIHeroDetailInfo.create(self.classId);
        UIMgr.getCurrentScene():addForm(uiHeroInfo);
    end

    -- 关闭当前界面
    UIMgr.getCurrentScene():removeFormByName("UIHeroGainEffect");
end

-- 注册点击事件
function UIHeroGainEffect:registerTouchEvent(node)
    -- 注册返回按钮点击事件
    local btnBack = findChildByName(node, "BT/btn_back");
    local function onBackClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            AudioM.playFx("button_click");
            -- 显示英雄详细信息
            self:showDetail();
        end
    end
    AddBackEventHandler(self, btnBack, onBackClick);
    TextStyleM.setTextStyle(btnBack, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnBack:setTitleText(getLocStr("btn_text_back"));

    -- 注册任意地方点击返回事件
    local btnBgBack = findChildByName(self.node, "bg");
    local function onBgBackClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            AudioM.playFx("button_click");
            -- 显示英雄详细信息
            self:showDetail();
        end
    end
    btnBgBack:addTouchEventListener(onBgBackClick);

    -- 注册分享按钮点击事件
    local btnShare = findChildByName(self.node, "BT/btn_share");
    local hintNode = findChildByName(btnShare, "hint");
    local function onShareClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            AudioM.playFx("button_click");

            self:doShare();
        end
    end
    btnShare:addTouchEventListener(onShareClick);
    TextStyleM.setTextStyle(btnShare, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnShare:setTitleText(getLocStr("btn_text_share"));
    checkBlueBubbleStatus(hintNode, true);

    if isShareFuncOpen() then
        btnShare:setVisible(true);
    else
        btnShare:setVisible(false);
    end
end

-- 分享
function UIHeroGainEffect:doShare()
    local title   = getLocStr("slime_share_title");
    local content = getLocStr("slime_share_content");
    local shareTitle = "";
    local shareContent = "";
    if isOverseaPackage() then
        -- 海外版本
        shareTitle = string.format(getLocStr("fb_slime_share_title"), PetM.query(self.classId, "name"));
        shareContent = PetM.query(self.classId, "desc");
    else
        -- 国内版本
        shareContent = string.format(getLocStr("cn_slime_share_desc"), PetM.query(self.classId, "name"));
    end

    local extra = { ["title"] = shareTitle, ["desc"] = shareContent, ["classId"] = self.classId, };

    showShareForm(SHARE_TYPE_SLIME, title, content, extra);
end

-- 适配
function UIHeroGainEffect:resize()
    local node = self.node;

    -- bg居中显示并充满整个屏幕
    AlignM.fitToScreen(node, "bg");

    -- CT居中并缩放至合适大小
    AlignM.alignToCenter(node, "CT");

    -- BT置于底部并缩放至合适大小
    AlignM.alignToBottomCenter(node, "BT");
end

-- 重绘
function UIHeroGainEffect:redraw()
    local CT = self.node:getChildByName("CT");
    local heroNameLabel  = findChildByName(CT, "name");
    local heroTalkLabel  = findChildByName(CT, "talk");

    -- 显示英雄名字
    local heroConfig = PetM.query(self.classId);
    heroNameLabel:setString(PetM.query(self.classId, "name") .. "!");
    heroNameLabel:setTextColor(TextStyleM.TEXT_COLOR_BROWN_YELLOW);
    self:showTitleEffect(heroNameLabel);

    -- 显示英雄简介
    TextStyleM.setTextStyle(heroTalkLabel, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BRIGHT_YELLOW);
    heroTalkLabel:setString(heroConfig.talk);

    -- 创建英雄模型
    self:createHeroModel();

    -- 重绘招募部分信息
    self:redrawPetRecruitNode();
end

-- 重绘招募部分信息
function UIHeroGainEffect:redrawPetRecruitNode()
    local petNode = findChildByName(self.node, "CT/pet_recruit");
    local petTop  = findChildByName(petNode, "TOP");
    local petCT   = findChildByName(petNode, "CT");
    local petBT   = findChildByName(petNode, "BT");

    petTop:setVisible(false);
    petCT:setVisible(false);
    petBT:setVisible(false);

    -- 获取阵营信息
    local campId  = CampM.getPetCamp(self.classId);

    -- 背景
    local bg = findChildByName(petNode, "bg/hero_bg");
    local campIconPath = getHeroBgIconPath(campId);
    bg:loadTexture(campIconPath);

    -- 效果
    local effectImg = findChildByName(petNode, "bg/effect_img");
    effectImg:loadTexture(getMercenaryCampIconPath("effect" .. campId));
    effectImg:stopAllActions();
    effectImg:removeAllChildren();
    local effectList = {1287, 1286, 1288, 1289};
    playEffect(effectImg, effectList[campId], 300, 250, nil, 2.35);
end

-- 英雄名字特效
function UIHeroGainEffect:showTitleEffect(textNode)
    local fontSize = 70 * getLocFontSizeMulti();
    local bgPath = "images/ui/text_mask/text_mask.png";

    -- 生成艺术字并旋转飞入
    genArtWordAndRotateFlyIn(textNode, fontSize, bgPath);
end

-- 创建英雄模型
function UIHeroGainEffect:createHeroModel()
    local headImg = findChildByName(self.node, "CT/model_node");
    local lightImg = findChildByName(self.node, "CT/light_bg");
    lightImg:setVisible(false);

    local modelId = PetM.query(self.classId, "default_model");
    local heroAnim = HeroDisplay.create(modelId);
    headImg:addChild(heroAnim);
end
